//
//  RootViewController.m
//  12.30_homework_豆瓣电影MVC
//
//  Created by dllo on 15/12/30.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "RootViewController.h"
#import "ActivityCell.h"
#import "ActivityModel.h"
#import "UIImageView+WebCache.h"
#import "AcitivityDetail.h"

@interface RootViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *arrModel;
@property (nonatomic, retain) UITableView *tableView;
// 存放是否收藏的数组
@property (nonatomic, retain) NSMutableArray *arrIndex;

@end

@implementation RootViewController

- (void)dealloc {
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"活动";
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"activity" ofType:@"plist"];
    NSArray *arrData = [NSArray arrayWithContentsOfFile:path];
    
    self.arrModel = [NSMutableArray array];
    for (NSDictionary *dic in arrData) {
        ActivityModel *activityModel = [ActivityModel activityModel];
        [activityModel setValuesForKeysWithDictionary:dic];
        [_arrModel addObject:activityModel];
    }

    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:(UITableViewStylePlain)];
    [self.view addSubview:_tableView];
    [_tableView release];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [self.tableView registerClass:[ActivityCell class] forCellReuseIdentifier:@"cell"];
    
//    [_tableView registerNib:[UINib nibWithNibName:@"ActivityCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
    
    // 是否收藏的数组
    self.arrIndex = [NSMutableArray array];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arrModel.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    ActivityModel *activtyModel = _arrModel[indexPath.row];
    NSString *str1 = activtyModel.begin_time;
    NSString *str2 = activtyModel.end_time;
    cell.title.text = activtyModel.title;
    cell.time.text = [NSString stringWithFormat:@"%@ -- %@", [str1 substringWithRange:NSMakeRange(5, 11)], [str2 substringWithRange:NSMakeRange(5, 11)]];
    cell.address.text = activtyModel.address;
    cell.category_name.text = [NSString stringWithFormat:@"类型：%@",  activtyModel.category_name];
    cell.wisher_count.text = [NSString stringWithFormat:@"感兴趣：%@", [activtyModel.wisher_count stringValue]];
    cell.participant_count.text = [NSString stringWithFormat:@"参加：%@", [activtyModel.participant_count stringValue]];
    [cell.picImage sd_setImageWithURL:[NSURL URLWithString:activtyModel.image]];
    
//    cell.MyTitle.text = activtyModel.title;
//    cell.MyTime.text = [NSString stringWithFormat:@"%@ -- %@", [str1 substringWithRange:NSMakeRange(5, 11)], [str2 substringWithRange:NSMakeRange(5, 11)]];
//    cell.MyAddress.text = activtyModel.address;
//    cell.MyCategory_name.text = [NSString stringWithFormat:@"类型：%@",  activtyModel.category_name];
//    cell.MyWisher_count.text = [NSString stringWithFormat:@"感兴趣：%@", [activtyModel.wisher_count stringValue]];
//    cell.MyParticipant_count.text = [NSString stringWithFormat:@"参加：%@", [activtyModel.participant_count stringValue]];
//    [cell.MyPic_Image sd_setImageWithURL:[NSURL URLWithString:activtyModel.image]];

    
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    AcitivityDetail *acDetailVC = [[AcitivityDetail alloc] init];
    
    acDetailVC.indexPath = indexPath;
    
    ActivityModel *model = _arrModel[indexPath.row];
    acDetailVC.name = model.title;
    
    //// 属性传值 将数组传过来 用来收藏的时候根据indexpath取model 加入数据库
    acDetailVC.arrModel = _arrModel;
    // 推过去之后隐藏
    acDetailVC.hidesBottomBarWhenPushed = YES;
    
    
//    if ([_arrIndex containsObject:indexPath]) {
//        
//        acDetailVC.flag = 1;
//        
//    }
//    
//    acDetailVC.block = ^(NSIndexPath *index){
//
//        if ([_arrIndex containsObject:index]) {
//            
//            acDetailVC.flag = 1;
//            
//        }else {
//            
//            [self.arrIndex addObject:index];
//            
//            acDetailVC.flag = 1;
//        }
//        NSLog(@"%@", _arrIndex);
//    };
    
    
    [self.navigationController pushViewController:acDetailVC animated:YES];
    [acDetailVC release];
    
    // 隐藏之后就不出来了
//    self.tabBarController.tabBar.hidden = NO;
    // 应该确定一个对象隐藏
//    self.tabBarController.hidesBottomBarWhenPushed = YES;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
